
package com.zjff.myshop.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zjff.myshop.entity.Cart;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.front.pojo.form.BatchIdsForm;
import com.zjff.myshop.front.pojo.form.CartInsertForm;
import com.zjff.myshop.front.pojo.form.CartUpdateForm;
import com.zjff.myshop.front.pojo.vo.CartVO;
import com.zjff.myshop.front.service.CartService;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@Api(value = "v1", tags = "05.购物车模块")
@RequestMapping("/app/v1/cart")
public class CartController {
    private static final Logger logger = LoggerFactory.getLogger(CartController.class);
    @Resource
    private CartService cartService;

    /**
     * 增加（单个）
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "增加商品到购物车", notes = "传参为商品id、数量")
    @ApiImplicitParam(name = "form", value = "增加购物车参数", dataType = "CartInsertForm", required = true)
    @SaCheckLogin
    @PostMapping("/insert")
    public Result insert(@RequestBody @Valid CartInsertForm form) {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        Cart cart = new Cart();
        BeanUtils.copyProperties(form, cart);
        cart.setUserId(userId);
        if (cartService.insert(cart)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（单个）
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除单个购物项", notes = "传参为购物项id")
    @ApiImplicitParam(name = "id", value = "购物车项id", dataType = "Integer", required = true)
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        if (cartService.deleteById(id, userId)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（批量）
     */
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "删除多个购物车项", notes = "批量删除购物车项信息")
    @ApiImplicitParam(name = "form", value = "批量删除参数", dataType = "BatchIdsForm", required = true)
    @SaCheckLogin
    @DeleteMapping(value = "/batch")
    public Result deleteBatchByIds(@RequestBody @Valid BatchIdsForm form) {
        //检查参数
        if (form == null || form.getIds().length < 1) {
            throw new BusinessException("参数错误");
        }
        if (cartService.deleteBatch(form.getIds())) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除我的购物车中的全部商品（清空）
     */
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "删除我的购物车中的全部商品", notes = "清空我的购物车中的全部商品")
    @SaCheckLogin
    @DeleteMapping(value = "/allByMy")
    public Result deleteAllByMy() {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        if (cartService.deleteByUserId(userId)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更改（单个）
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改购物项数据", notes = "参数为购物项id（必选）、数量count、是否勾选isChecked")
    @ApiImplicitParam(name = "form", value = "更新购物车参数", dataType = "CartUpdateForm", required = true)
    @SaCheckLogin
    @PostMapping("/update")
    public Result update(@RequestBody @Valid CartUpdateForm form) {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        Cart cart = new Cart();
        BeanUtils.copyProperties(form, cart);
        cart.setUserId(userId);
        if (cartService.update(cart)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更改我的购物车为全勾选或全不选
     */
    @ApiOperationSupport(order = 21)
    @ApiOperation(value = "更改我的购物项数据为全勾选或全不选", notes = "参数为是否勾选isChecked")
    @ApiImplicitParam(name = "isChecked", value = "true-全选，false-全不选", dataType = "Boolean", required = true)
    @SaCheckLogin
    @PostMapping("/updateIsCheckedAllByMy/{isChecked}")
    public Result updateIsCheckedAllByMy(@PathVariable Boolean isChecked) {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        if (cartService.updateIsCheckedAllByUserId(isChecked, userId)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 查询我的购物车列表
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询我的购物车列表", notes = "网页移动端不分页，无参数")
    @SaCheckLogin
    @GetMapping("/listByMy")
    public Result<List<CartVO>> listByMy() {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        return Result.ok(cartService.selectVOList(map));
    }

    /**
     * 查询我的购物车列表ForIsChecked(已勾选)
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询我的购物车列表(已勾选商品项）", notes = "网页移动端不分页，无参数")
    @SaCheckLogin
    @GetMapping("/listIsCheckedByMy")
    public Result<List<CartVO>> listIsCheckedByMy() {
        //当前登录用户Id
        Integer userId = StpUtil.getLoginIdAsInt();
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("isChecked", true);
        return Result.ok(cartService.selectVOList(map));
    }
}
